package my.tutorial1;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTutorial1Activity extends Activity implements
		OnClickListener {
	
	public static JSONArray phpRecv(String address,
			ArrayList<NameValuePair> param){
		return invokePhpDocument(address,param,true);
	}
	
	public static void phpSend(String address,
			ArrayList<NameValuePair> param){
		invokePhpDocument(address,param,false);
	}

	public static JSONArray invokePhpDocument(String address,
			ArrayList<NameValuePair> param, boolean returnsJSON) {
		String result = "";
		// the year data to send

		InputStream is = null;

		// http post
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(address);
			httppost.setEntity(new UrlEncodedFormEntity(param));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}
		// convert response to string
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		// parse json data
		if (returnsJSON) {
			try {
				JSONArray jArray = new JSONArray(result);
				return jArray;
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString());
			}
		}

		return null;
	}
	
	public void refreshMessages(){
		Editable e = print.getText();
		e.clear();
		String address = "http://192.168.1.136/RLRPGMiddleware/getConversation.php";
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("id", "1"));

		// parse json data
		try {
			JSONArray jArray = AndroidTutorial1Activity.phpRecv(
					address, nameValuePairs);
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				e.append(json_data.getString("description"));
			}
		} catch (JSONException ex) {
			Log.e("log_tag", "Error parsing data " + ex.toString());
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		refButton = (Button) this.findViewById(R.id.button1);
		sendButton = (Button) this.findViewById(R.id.button2);
		print = (EditText) this.findViewById(R.id.editText1);
		con = (EditText) this.findViewById(R.id.editText2);

		refButton.setOnClickListener(this);
		sendButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		if (arg0 == refButton) {
			refreshMessages();
		} else {

		}

	}

	private Button refButton;
	private Button sendButton;
	private EditText print;
	private EditText con;
}